package com.corx.network.mysqlstorage.repository;

import com.corx.network.mysqlstorage.common.JdbcPagingSupport;
import com.corx.network.mysqlstorage.common.PageQuery;
import com.corx.network.mysqlstorage.common.PageResult;
import com.corx.network.mysqlstorage.common.SqlQueryBuilder;
import com.corx.network.mysqlstorage.model.Device;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.NonNull;

import java.util.List;
import java.util.Optional;

/**
 * 设备 JDBC Repository
 * @author gx
 * @version 1.0.0
 */
public class JdbcDeviceRepository extends JdbcPagingSupport<Device> implements DeviceRepository {

    public JdbcDeviceRepository(JdbcTemplate jdbcTemplate) {
        super(jdbcTemplate);
    }

    @Override
    protected RowMapper<Device> getRowMapper() {
        return (rs, rowNum) -> {
            Device data = new Device();
            data.setId(rs.getLong("id"));
            data.setDeviceCode(rs.getString("device_code"));
            data.setName(rs.getString("name"));
            data.setType(rs.getString("type"));
            data.setNetworkId(rs.getLong("network_id"));
            data.setStatus(rs.getString("status"));
            data.setState(rs.getString("state"));
            data.setCreateTime(rs.getTimestamp("create_time"));
            data.setUpdateTime(rs.getTimestamp("update_time"));
            return data;
        };
    }

    @Override
    public Optional<Device> findById(@NonNull Long id) {
        List<Device> list = jdbcTemplate.query("SELECT * FROM corx_device WHERE id = ?", getRowMapper(), id);
        return Optional.ofNullable(DataAccessUtils.singleResult(list));
    }

    @Override
    public Optional<Device> findByDeviceCode(String deviceCode) {
        List<Device> list = jdbcTemplate.query("SELECT * FROM corx_device WHERE device_code = ?", getRowMapper(), deviceCode);
        return Optional.ofNullable(DataAccessUtils.singleResult(list));
    }

    @Override
    public List<Device> findByNetworkId(Long networkId) {
        return jdbcTemplate.query("SELECT * FROM corx_device WHERE network_id = ?", getRowMapper(), networkId);
    }

    @Override
    public PageResult<Device> findByPage(Device model, PageQuery pageQuery) {
        SqlQueryBuilder queryBuilder = new SqlQueryBuilder("SELECT * FROM corx_device");
        if (model.getDeviceCode() != null) {
            queryBuilder.whereEquals("device_code", model.getDeviceCode());
        }
        if (model.getName() != null && !model.getName().isEmpty()) {
            queryBuilder.whereLike("name", model.getName());
        }
        if (model.getType() != null && !model.getType().isEmpty()) {
            queryBuilder.whereEquals("type", model.getType());
        }
        if (model.getStatus() != null && !model.getStatus().isEmpty()) {
            queryBuilder.whereEquals("status", model.getStatus());
        }
        if (model.getState() != null && !model.getState().isEmpty()) {
            queryBuilder.whereEquals("state", model.getState());
        }
        if (model.getNetworkId() != null) {
            queryBuilder.whereEquals("network_id", model.getNetworkId());
        }
        if (pageQuery.hasSort()) {
            queryBuilder.orderBy(pageQuery.orderBy());
        }
        String baseSql = queryBuilder.getSql();
        Object[] params = queryBuilder.getParams();
        return super.findByPage(baseSql, params, pageQuery);
    }

    @Override
    public void save(Device model) {
        String sql = "INSERT INTO corx_device (device_code, `name`, `type`, network_id,  status, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW())";
        jdbcTemplate.update(sql, model.getDeviceCode(), model.getName(), model.getType(), model.getNetworkId(),  model.getStatus(), model.getState());
    }

    @Override
    public void update(Device model) {
        String sql = "UPDATE corx_device SET device_code = ?, `name` = ?, `type` = ?, network_id = ?, `status` = ?, `state` = ?, update_time = NOW() WHERE id = ?";
        jdbcTemplate.update(sql, model.getDeviceCode(), model.getName(), model.getType(), model.getNetworkId(),  model.getStatus(), model.getState(), model.getId());
    }

    @Override
    public void delete(Long id) {
        jdbcTemplate.update("DELETE FROM corx_device WHERE id = ?", id);
    }
}
